package com.penghaisoft.microwms.producthand.web.controller.insidewarehouse;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProStorageSnDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.common.wms.model.ProStorageSnChkHis;
import com.penghaisoft.microwms.product.service.IProStorageSnService;

@RestController
@RequestMapping(value = "/pro/lockhand")
public class ProLockHandController extends BaseController {
	@Autowired
	private IProStorageSnService proStorageSnService;

	@PostMapping("lockmessage")
	public HandResp lockMessage(@RequestBody ProStorageSn proStorageSn) {
		HandResp result = new HandResp();
		// Code先置为1SUCCESS
		result.setCode(Constant.SUCCESS);
		// 根据条码查询
		proStorageSn.setActiveFlag("1");
		List<ProStorageSn> list = proStorageSnService.queryByAny(proStorageSn);
		if (list.size() <= 0) {
			result.setMsg("该条码不存在");
			result.setCode(Constant.FAIL);
		} else {
			if (list.get(0).getBarcodeStatus().equals("2")) {
				result.setMsg("该条码已出库");
				result.setCode(Constant.FAIL);
			} else if (list.get(0).getBarcodeStatus().equals("1")) {
				result.setMsg("该条码已冻结");
				result.setCode(Constant.FAIL);
			} else {
				// 返回信息
				result.setList(list);
				result.setMsg("该条码可进行冻结，请确认");
			}
		}
		return result;
	}

	@PostMapping("lockconfig")
	public HandResp lockConfig(@RequestBody ProStorageSnDto proStorageSnDto) {
		HandResp result = new HandResp();
		// Code先置为1SUCCESS
		result.setCode(Constant.SUCCESS);
		ProStorageSn barcodeStorageSn = new ProStorageSn();
		barcodeStorageSn.setBarcode(proStorageSnDto.getBarcode());
		barcodeStorageSn.setActiveFlag("1");
		// 根据条码查询
		List<ProStorageSn> list = proStorageSnService
				.queryByAny(barcodeStorageSn);
		if (list.size() <= 0) {
			result.setMsg("该条码不存在");
			result.setCode(Constant.FAIL);
		} else {
			if (list.get(0).getBarcodeStatus().equals("2")) {
				result.setMsg("该条码已出库");
				result.setCode(Constant.FAIL);
			} else if (list.get(0).getBarcodeStatus().equals("1")) {
				result.setMsg("该条码已冻结");
				result.setCode(Constant.FAIL);
			} else {
				// 开始冻结
				Resp resp = new Resp();
				ProStorageSn proStorageSn = new ProStorageSn();
				proStorageSn.setStorageSnId(list.get(0).getStorageSnId());
				proStorageSn.setBarcode(proStorageSnDto.getBarcode());
				proStorageSn.setBarcodeStatus("1");// 冻结
				proStorageSn.setLockReason(proStorageSnDto.getLockReason());// 0手工冻结1质量冻结
				proStorageSn.setLastModifiedBy(proStorageSnDto.getLoginName());
				proStorageSn.setGmtModified(new Date());
				resp = proStorageSnService.updateProStorageSn(proStorageSn);
				result.setCode(resp.getCode());
				if (resp.getCode().equals("0")) {
					result.setMsg(resp.getMsg());
				}
			}
		}
		return result;
	}

	/** 
	* @Title: checkInfo 
	* @Description: 成品质检 查看详情 
	* @param @param proStorageSn
	* @param @return  参数说明 
	* @return HandResp    返回类型
	* @author luoteng 
	* @date 2018年10月26日 下午3:59:36 
	* @throws 
	*/
	@PostMapping("check/info")
	public HandResp checkInfo(@RequestBody ProStorageSn proStorageSn) {
		HandResp result = new HandResp();
		// Code先置为1SUCCESS
		result.setCode(Constant.SUCCESS);
		// 根据条码查询
		proStorageSn.setActiveFlag("1");
		List<ProStorageSn> list = proStorageSnService.queryByAny(proStorageSn);
		if (list ==null || list.isEmpty()) {
			result.setMsg("该条码不存在！");
			result.setCode(Constant.FAIL);
		} else {
			ProStorageSn tmp = list.get(0);
			
			if (tmp.getBarcodeStatus().equals("2")) {
				result.setMsg("该条码已出库！");
				result.setCode(Constant.FAIL);
			} else if (tmp.getBarcodeStatus().equals("1") && tmp.getCheckStatus().equals("0")) {
//				0：可用 1：冻结 2：已出库 3：抽检冻结
//				0：未质检 1：质检合格 2：质检不合格
				result.setMsg("该条码已冻结，无需再质检!");
				result.setCode(Constant.FAIL);
			} else {
				// 返回信息
				result.setList(list);
				result.setMsg("该条码可进行冻结，请确认！");
			}
		}
		return result;
	}

	/** 
	* @Title: checkConfirm 
	* @Description: 成品质检扫码确认
	* @param @param proStorageSnDto
	* @param @return  参数说明 
	* @return HandResp    返回类型
	* @author luoteng 
	* @date 2018年10月26日 下午4:03:56 
	* @throws 
	*/
	@PostMapping("check/confirm")
	public HandResp checkConfirm(@RequestBody ProStorageSnDto proStorageSnDto) {
		Date now = new Date();
		HandResp result = new HandResp();
		// Code先置为1SUCCESS
		result.setCode(Constant.SUCCESS);
		ProStorageSn barcodeStorageSn = new ProStorageSn();
		barcodeStorageSn.setBarcode(proStorageSnDto.getBarcode());
		barcodeStorageSn.setActiveFlag("1");
		// 根据条码查询
		List<ProStorageSn> list = proStorageSnService.queryByAny(barcodeStorageSn);
		if (list == null || list.isEmpty()) {
			result.setMsg("该条码不存在！");
			result.setCode(Constant.FAIL);
		} else {
			ProStorageSn tmp = list.get(0);
			
			if (tmp.getBarcodeStatus().equals("2")) {
				result.setMsg("该条码已出库！");
				result.setCode(Constant.FAIL);
			} else if (tmp.getBarcodeStatus().equals("1") && tmp.getCheckStatus().equals("0")) {
//				0：可用 1：冻结 2：已出库 3：抽检冻结
//				0：未质检 1：质检合格 2：质检不合格
				result.setMsg("该条码已冻结，无需再质检!");
				result.setCode(Constant.FAIL);
			} else {
				// 开始冻结
				Resp resp = new Resp();
				
				ProStorageSnChkHis his = new ProStorageSnChkHis();
				his.setBarcode(tmp.getBarcode());
				his.setAmount(tmp.getAmount());
				his.setUnit(tmp.getUnit());
				his.setContractNo(tmp.getContractNo());
				his.setPurchaseNo(tmp.getPurchaseNo());
				his.setModel(tmp.getModel());
				his.setModelDesc(tmp.getModelDesc());
				his.setModelType(tmp.getModelType());
				his.setType(tmp.getType());
				his.setPrdOrder(tmp.getPrdOrder());
				his.setProManuDate(tmp.getProManuDate());
				his.setProPlanDate(tmp.getProPlanDate());
				his.setWhCode(tmp.getWhCode());
				his.setWdCode(tmp.getWdCode());
				his.setWlCode(tmp.getWlCode());
				his.setWaCode(tmp.getWaCode());
				his.setCheckStatus(proStorageSnDto.getCheckStatus());
				his.setGmtCheck(now);
				his.setGmtCreate(now);
				his.setCreateBy(proStorageSnDto.getLoginName());
				his.setActiveFlag("1");
				
				ProStorageSn proStorageSn = new ProStorageSn();
				proStorageSn.setStorageSnId(tmp.getStorageSnId());
				proStorageSn.setBarcode(proStorageSnDto.getBarcode());
				if(proStorageSnDto.getCheckStatus() != null){
//					0：未质检 1：质检合格 2：质检不合格
					if(proStorageSnDto.getCheckStatus().equals("2")){
//						0：可用 1：冻结 2：已出库 3：抽检冻结
						proStorageSn.setBarcodeStatus("1");// 冻结
						proStorageSn.setLockReason(proStorageSnDto.getLockReason());
						
						his.setBarcodeStatus("1");
						his.setLockReason(proStorageSnDto.getLockReason());
					}else if(proStorageSnDto.getCheckStatus().equals("1")){
//						0：可用 1：冻结 2：已出库 3：抽检冻结
						proStorageSn.setBarcodeStatus("0");// 冻结
						proStorageSn.setLockReasonNull("空");
						
						his.setBarcodeStatus("0");
						his.setLockReason(null);
					}else{
						result.setMsg("参数传递错误!");
						result.setCode(Constant.FAIL);
						return result;
					}
				}else{
					result.setMsg("参数传递错误!");
					result.setCode(Constant.FAIL);
					return result;
				}

				proStorageSn.setCheckStatus(proStorageSnDto.getCheckStatus());
				proStorageSn.setGmtCheck(now);
				proStorageSn.setLastModifiedBy(proStorageSnDto.getLoginName());
				proStorageSn.setGmtModified(now);
				
				resp = proStorageSnService.updateProStorageSnNew(proStorageSn, his);
				
				result.setCode(resp.getCode());
				if (resp.getCode().equals("0")) {
					result.setMsg(resp.getMsg());
				}
			}
		}
		return result;
	}
}
